package com.kfm.jdbc.day0406;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSender {

    public static void main(String[] args) {
        try {
            // ip
            InetAddress localhost = InetAddress.getLocalHost();
//            InetAddress localhost = InetAddress.getByName("10.0.3.198");
            int port = 8888;
            DatagramSocket datagramSocket = new DatagramSocket(8080, localhost);
            // 创建发送端
            Scanner in = new Scanner(System.in);
            while (true){
                System.out.println("请输入要发送的消息:");
                String msg = in.nextLine();
                if ("end".equals(msg)){
                    break;
                }
                DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, localhost, port);
                // 发送
                datagramSocket.send(packet);
            }

        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
